home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part1 / 7604 < prev    next >
Encoding:
Text File  |  1996-08-05  |  2.8 KB  |  99 lines

  1. Path: ix.netcom.com!netnews
  2. From: regmia@ix.netcom.com
  3. Newsgroups: comp.lang.c,comp.lang.c++
  4. Subject: Re: Passing functions as parameters... inconsistent behaviour
  5. Date: Sat, 24 Feb 1996 07:18:21 GMT
  6. Organization: Netcom
  7. Message-ID: <4gme0e$atu@cloner4.netcom.com>
  8. References: <4glbau$tge@inet.up.ac.za>
  9. NNTP-Posting-Host: ix-lv5-10.ix.netcom.com
  10. X-NETCOM-Date: Fri Feb 23 11:16:30 PM PST 1996
  11. X-Newsreader: Forte Free Agent 1.0.82
  12.  
  13. In your code fragment you must declare what type of parameters are
  14. going to be sent to the function, if any parameters, for example:
  15.  
  16. >void test(float (*passedfunc)(float)) {
  17. >   passedfunc(1);      /*see    ------- */
  18. >}
  19.  
  20. >float passed(float a){
  21. >    return 1.0;
  22. >}
  23.  
  24. >int main(void) {
  25. >    test( passed );
  26. >    return 0;
  27. >}
  28.  
  29. See what I mean, I guarantee it will work for you each and every time.
  30. :)
  31.  
  32.  
  33. Rudolph Pienaar <rudolph@pangea.ee.up.ac.za> wrote:
  34.  
  35. >Hello all -
  36.  
  37. >I've noticed strange compiler behaviour when changing the *type* of data in the
  38. >parameter list of a passed function...
  39.  
  40. >Consider this simple code fragment:
  41.  
  42. >/***********************************/
  43. >void test(float (*passedfunc)()) {
  44. >   passedfunc(1);
  45. >}
  46.  
  47. >float passed(float a){
  48. >    return 1.0;
  49. >}
  50.  
  51. >int main(void) {
  52. >    test( passed );
  53. >    return 0;
  54. >}
  55. >/***********************************/
  56.  
  57. >Reasonably straightforward - the function "passed" is sent on to another
  58. >function, "test". "test", in turn calls the function that was passed to it.
  59.  
  60. >This little piece of meaningless code compiles without any hitches, yet when I
  61. >change the type of "a" in passed's parameter list to float, ie:
  62.  
  63. > float passed(int a) ---> float passed(float a)
  64. >              ^^^                      ^^^^^
  65.  
  66. >and recompile, I get the following warning:-
  67.  
  68. >ptr.c: In function `main':
  69. >ptr.c:17: warning: passing arg 1 of `test' from incompatible pointer type
  70.  
  71. >Of course, this is just a sample... the general rule seems to be that if a
  72. >function is passed to another, and if this passed function has any parameters
  73. >of its own other than *int* (and this includes user defined types), compilation
  74. >results in a similar warning.
  75.  
  76. >I have received identical results with gcc / cc on the following platforms:
  77. >> An i486 running Linux (1.1.59)
  78. >> A RS6000 running AIX 3.2.4
  79. >> A SunSparc running SunOS 5.3
  80.  
  81. >Although I haven't noticed any strange behaviour in running programs after this
  82. >warning, I have no idea why the compiler generates this message.
  83.  
  84. >Any comments, explanations, suggestions, corrections will be much appreciated.
  85.  
  86. >Thanks
  87. >Rudolph
  88.  
  89. >________________________________________________________________________
  90. >e-mail:       rudolph@pangea.ee.up.ac.za
  91. >In Real Life: Rudolph Pienaar
  92. >Organization: Electrical and Electronic Engineering
  93. >              University of Pretoria      
  94. >              South Africa                
  95. >________________________________________________________________________
  96.  
  97.  
  98.  
  99.